import 'dart:math';

import 'package:flutter/material.dart';
import 'package:jinke/utils/util.dart';

class AddNumWidget extends StatefulWidget {
  AddNumWidget({
    Key? key,
    required this.title,
    required this.subTitle,
    required this.onChanged,
    this.floatCount = 0,
  }) : super(key: key);
  final String title;
  final String subTitle;
  final Function(num) onChanged;
  final int floatCount;
  @override
  State<AddNumWidget> createState() => _AddNumWidgetState();
}

class _AddNumWidgetState extends State<AddNumWidget> {
  @override
  void initState() {
    super.initState();
    textCtrl.text = 0.toStringAsFixed(widget.floatCount);
  }

  final textCtrl = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.r),
      height: 50.r,
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Text(
                widget.title,
                style: TextStyle(color: ColorsUtil.title, fontSize: 15.sp),
              ),
              Text(
                widget.subTitle,
                style: TextStyle(color: ColorsUtil.content, fontSize: 13.sp),
              ),
            ],
          ),
          Row(
            children: [
              InkWell(
                onTap: () {
                  textCtrl.text = max(
                          (numVal(textCtrl.text) -
                              1 / 10.power(widget.floatCount)),
                          0)
                      .toStringAsFixed(widget.floatCount);
                },
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.grey,
                      width: 0.5,
                    ),
                    borderRadius: BorderRadius.circular(2.r),
                  ),
                  child: Icon(
                    Icons.remove,
                    color: ColorsUtil.content,
                    size: 28.r,
                  ),
                ),
              ),
              Container(
                width: 60.r,
                child: TextField(
                  controller: textCtrl,
                  onChanged: (value) {
                    widget.onChanged(numVal(value));
                  },
                  textAlign: TextAlign.center,
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(
                    border: InputBorder.none,
                    isCollapsed: true,
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  textCtrl.text =
                      (numVal(textCtrl.text) + 1 / 10.power(widget.floatCount))
                          .toStringAsFixed(widget.floatCount);
                  setState(() {});
                },
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.grey,
                      width: 0.5,
                    ),
                    borderRadius: BorderRadius.circular(2.r),
                  ),
                  child: Icon(
                    Icons.add,
                    color: ColorsUtil.content,
                    size: 28.r,
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}
